package iscript.airFramework.util 
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import iscript.coreFramework.data.fzip.FZip;

	public class FileHelper 
	{
		
		public function FileHelper() 
		{
			
		}

		static public function fixPath(path:String):String {
			return path.replace('\\','/');
			}

		static  public function open(file:File,encoding:String="UTF-8"):String {
			var  txt:String;
		    var  fs:FileStream  = new FileStream();
			fs.open(file, FileMode.READ);
			txt = fs.readMultiByte(fs.bytesAvailable, encoding);
			fs.close();
			return txt;
		}
		
		
		static  public function write(file:File,data:String,encoding:String="UTF-8"):void {
		    var  fs:FileStream  = new FileStream();
			//fs.open(file, FileMode.WRITE);
			fs.openAsync(file, FileMode.WRITE);
			fs.writeMultiByte(data, encoding);
			fs.close();
		}
		
		static public function IsFile(file:File,expandFile:String):Boolean {
			   return file.name.indexOf(expandFile) != -1
			}
		
		
		/**
		 * 获得文件夹列表
		 * @param	file
		 * @return
		 */
		static public function getFolderList(file:File):Vector.<File> {
			 var folders:Vector.<File> = new Vector.<File>();
			 var arr:Array = file.getDirectoryListing();
			     for each( var item:File in arr) {
				    if (item.isDirectory) {
						folders.push(item);
						}
			     }			 
			 return folders;
	      }
		
		 /**
		  * 获得所有文件包括文件夹和文件
		  * @param	file
		  * @param	filter
		  * @return
		  */
		 static public function getFileChilds(file:File,filter:String="*"):Vector.<File>  {
			  var source:Vector.<File> = new Vector.<File>();
              var extendName:String;
			  var arr:Array = file.getDirectoryListing();
			  var filterFiles:Array;
			  if (filter != "*") filterFiles =  filter.split(",");
			  for each( var item:File in arr) {
				    if (item.isDirectory) {
						 getFiles(item, filter);
						  source.push(item)
						}else {
							
						  if (filter == "*") {
							 source.push(item)
							 }
						 	 if (filter != "*") {	
								 extendName = item.name.substr(item.name.length - 4, 4).toLocaleUpperCase();
								 for each (var f:String in filterFiles) 
								 {
									 if ( extendName ==  f) {
										   source.push(item);
										 }
								 }
							 }    
					    }
				 } 
				return source;
			}
		
        /**
         * 获得文件对象 不包括文件夹
         * @param	file
         * @param	filter
         * @return
         */
		static public function getFiles(file:File,filter:String="*"):Vector.<File>  {
			  var source:Vector.<File> = new Vector.<File>();
              var extendName:String;
			  var arr:Array = file.getDirectoryListing();
			  var filterFiles:Array;
			  if (filter != "*") filterFiles =  filter.split(",");
			  
			  for each( var item:File in arr) {
				    if (item.isDirectory) {
						// getFiles(item,filter);
						}else {
							
						  if (filter == "*") {
							 source.push(item)
							 }
						 if (filter != "*") {	
							 extendName = item.name.substr(item.name.length - 4, 4).toLocaleUpperCase();
							 for each (var f:String in filterFiles) 
							 {
								 if ( extendName ==  f.toLocaleUpperCase()) {
									   source.push(item);
									 }
							 }   
							 
							 /*if (extendName == "." + filter.toLocaleUpperCase()) {
									source.push(item);
									}*/
							 } 
					    }
				 } 
				return source;
			}
			
			
			
		
	}

}